package cn.liuding.pethome.org.service.impl;

import cn.liuding.pethome.basic.base.service.impl.BaseServiceImpl;
import cn.liuding.pethome.basic.util.PageList;
import cn.liuding.pethome.org.domain.Department;
import cn.liuding.pethome.org.mapper.DepartmentMapper;
import cn.liuding.pethome.org.query.DepartmengQuery;
import cn.liuding.pethome.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.lang.model.element.VariableElement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService{

    @Autowired
    private DepartmentMapper departMentMapper;


    @Override
    public void add(Department departMent) {
        departMentMapper.add(departMent);

        //判断是不是顶级部门
        if(departMent.getParent()==null){
            departMent.setDirPath("/"+departMent.getId());
            departMentMapper.update(departMent);
        }else{
            Department father = departMentMapper.loadById(departMent.getParent().getId());
            String fatherDirPath = father.getDirPath();
            departMent.setDirPath(fatherDirPath+"/"+departMent.getId());
            departMentMapper.update(departMent);
        }
    }
    /*
    @Override
    public void update(Department departMent) {
        departMentMapper.update(departMent);
    }


    @Override
    public void delete(Long id) {
        departMentMapper.delete(id);
    }

    @Override
    public Department loadById(Long id) {

        return departMentMapper.loadById(id);
    }

    @Override
    public List<Department> loadAll() {
        return departMentMapper.loadAll();
    }

    @Override
    public PageList<Department> pageList(DepartmengQuery query) {
        Long tatals = departMentMapper.queryCount(query);
        List<Department> listData = departMentMapper.queryData(query);
        PageList<Department> list = new PageList<Department>(listData, tatals);
        return list;
    }

    @Override
    public void deleteAll(List<Long> ids) {
        departMentMapper.deleteAll(ids);
    }*/

    @Override
    public List<Department> treeData() {

        List<Department> departments = departMentMapper.loadAll();
        /*Map<Long,Department> mapData=new HashMap<>();
        for (Department department:departments) {
            mapData.put(department.getId(),department);
        }*/
        /*Map<Long,Department> mapData=departments.stream().
                collect();*/
        Map<Long, Department> departmentMap = departments.stream().collect(Collectors.toMap((obj) -> {
            return obj.getId();
        }, (obj) -> {
            return obj;
        }));
        List<Department> roots = new ArrayList<>();
        for (Department department:departments) {
            if(department.getParent_id()==null){
                roots.add(department);
            }else{
                Department father = departmentMap.get(department.getParent_id());
                father.getChildren().add(department);
            }
        }
       return roots;
    }
}
